const express = require('express');
const router = express.Router();
// 导入multer模块 用于解析multipart/form-data 格式的表单数据
const multer = require('multer');
// 导入路径处理模块
const path = require('path');
// 创建multer实例  并通过dest属性指定文件的存放路径
const upload = multer({ dest: path.join(__dirname, '../uploads') });

// 导入路由处理函数模块
const articles_fn = require('../router_handler/articles_fn');
// 定义 发布新文章 路由
// upload.single() 是一个局部生效的中间件，用来解析 FormData 格式的表单数据
// 将文件类型的数据，解析并挂载到 req.file 属性中
// 将文本类型的数据，解析并挂载到 req.body 属性中
// 导入验证模块
const expressJoi = require('@escook/express-joi');
const { articles_pub_schema, articles_get_schema, articles_delete_schema, articles_getDetail_schema, articles_edit_schema } = require('../schema/articles_check');
router.post('/add', upload.single('cover_img'), expressJoi(articles_pub_schema), articles_fn.addArticle);
// 定义 获取文章列表 路由
router.get('/list', expressJoi(articles_get_schema), articles_fn.getArticleList);
// 定义 根据Id删除文章数据 路由
router.get('/delete/:id', expressJoi(articles_delete_schema), articles_fn.deleteArticle);
// 定义 根据Id获取文章 路由
router.get('/:id', expressJoi(articles_getDetail_schema), articles_fn.getArticleById);
// 定义 根据Id更新文章信息 路由
router.post('/edit', upload.single('cover_img'), expressJoi(articles_edit_schema), articles_fn.editArticleById);


module.exports = router;